package iscte.ai.kcgtd;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridviewAdapter extends BaseAdapter {

	private ArrayList<String> options;
	private ArrayList<Integer> optionsIcons;
	private Activity activity;
	
	public GridviewAdapter(Activity activity,ArrayList<String> options, ArrayList<Integer> optionsIcons) {
		super();
		this.options = options;
		this.optionsIcons = optionsIcons;
		this.activity = activity;
	}
	
	@Override
	public int getCount() {
		return options.size();
	}

	@Override
	public String getItem(int position) {
		return options.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public static class ViewHolder{
		public ImageView imgViewFlag;
		public TextView txtViewTitle;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder view;
		LayoutInflater inflator = activity.getLayoutInflater();
		
		if(convertView==null){
			view = new ViewHolder();
			convertView = inflator.inflate(R.layout.gridview_row, null);
			
			view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
			view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
			
			convertView.setTag(view);
		}else{
			view = (ViewHolder) convertView.getTag();
		}
		
		view.txtViewTitle.setText(options.get(position));
		view.imgViewFlag.setImageResource(optionsIcons.get(position));
		
		return convertView;
	}
	
}
